/**
 * Copyright &copy; 2020 <a href="https://github.com/somowhere/exam-system">exam-system</a> All rights reserved.
 */
package com.shanhesoft.java.modules.exam.web;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shanhesoft.java.common.core.constant.CommonConstants;
import com.shanhesoft.java.common.core.util.Result;
import com.shanhesoft.java.common.core.util.StringUtil;
import com.shanhesoft.java.common.core.vo.PageModel;
import com.shanhesoft.java.common.data.util.QueryWrapperUtil;
import com.shanhesoft.java.common.log.annotation.LogOperate;
import com.shanhesoft.java.common.security.util.SecurityUtil;
import com.shanhesoft.java.common.util.ExcelUtil;
import com.shanhesoft.java.common.web.resource.BaseResource;
import com.shanhesoft.java.modules.exam.domain.dto.*;
import com.shanhesoft.java.modules.exam.domain.vo.TQuestionBankVo;
import com.shanhesoft.java.modules.exam.service.*;

import lombok.extern.slf4j.Slf4j;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import lombok.AllArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.*;

/**
 * 题库管理Controller 题库管理
 * @author admin
 * @version 2020-09-17 18:43:07
 */
@RestController
@RequestMapping(value = "${application.admin-path}/exam/questionBank")
@Slf4j
@AllArgsConstructor
public class TQuestionBankResource extends BaseResource {

	private final TQuestionBankService service;
	private final TBankFillQueService fillQueService;
	private final TBankJudgeQueService judgeQueService;
	private final TBankSingleChoiceQueService singleChoiceQueService;
	private final TBankMultipleChoiceQueService multipleChoiceQueService;
	private final OjProblemsService ojProblemsService;

	/**
	 * @param id
	 * @return
	 */
	@GetMapping(CommonConstants.URL_ID_REGEX)
	@PreAuthorize("@pms.hasPermission('exam_tQuestionBank_view')")
	public Result get(@PathVariable String id) {
		log.debug("REST request to get Entity : {}", id);
		return  Result.buildOkData(service.getOneDto(id));
	}
	/**
	 * GET / : get all tQuestionBank.
	 *
	 * @return the Result with status 200 (OK) and with body all tQuestionBank
	 */

	@PreAuthorize("@pms.hasPermission('exam_tQuestionBank_view')")
	@GetMapping
	@ApiOperation(value = "题库管理查看")
	public Result<IPage<TQuestionBankVo>> getPage(TQuestionBankQueryCriteria tQuestionBankQueryCriteria) {
		return Result.buildOkData(service.findTreeList(tQuestionBankQueryCriteria));

//		QueryWrapper wrapper = QueryWrapperUtil.getWrapper(pm, tQuestionBankQueryCriteria);
//		return Result.buildOkData(service.page(pm, wrapper));
	}

	/**
	 * @return
	 */
	@GetMapping("/tree")
	public Result tree(TQuestionBankQueryCriteria tQuestionBankQueryCriteria) {
		return  Result.buildOkData(service.findBankTreeNode(tQuestionBankQueryCriteria));
	}

	/**
	 * POST / : Save a tQuestionBankDto.
	 *
	 * @param tQuestionBankDto the HTTP tQuestionBank
	 */
	@PreAuthorize("@pms.hasPermission('exam_tQuestionBank_edit')")
	@ApiOperation(value = "题库管理编辑")
	@PostMapping
	public Result save(@Valid @RequestBody TQuestionBankDto tQuestionBankDto) {
		log.debug("REST request to save TQuestionBankDto : {}", tQuestionBankDto);
		service.saveOrUpdate(tQuestionBankDto);
		return Result.buildOk("保存题库管理成功");

	}

	/**
	 * DELETE //:ids : delete the "ids" TQuestionBank.
	 *
	 * @param ids the id of the tQuestionBank to delete
	 * @return the Result with status 200 (OK)
	 */
	@PreAuthorize("@pms.hasPermission('exam_tQuestionBank_del')")
	@ApiOperation(value = "题库管理删除")
	@DeleteMapping
	public Result delete(@RequestBody Set<String> ids) {
		log.debug("REST request to delete TQuestionBank: {}", ids);
		service.removeByIds(ids);
		return Result.buildOk("删除题库管理成功");
	}

	/**
	 * GET / : 题库题库查询
	 *
	 * @return the Result with status 200 (OK) and with body all tQuestionBank
	 */

	@PreAuthorize("@pms.hasPermission('exam_tQuestionBank_view')")
	@GetMapping("/bankQue")
	@ApiOperation(value = "题库管理查看")
	public Result getBankQue(PageModel pm, String questionBankId,
							 String content, String type) {
		// 取4个题目列表
		if(StringUtils.isEmpty(type)) {
			type = "0"; // 默认查询单选题
		}

		if("0".equals(type)) {
			TBankSingleChoiceQueQueryCriteria criteria = new TBankSingleChoiceQueQueryCriteria();
			criteria.setQuestionBankId(questionBankId);
			criteria.setContent(content);
			QueryWrapper wrapper = QueryWrapperUtil.getWrapper(pm, criteria);
			return Result.buildOkData(singleChoiceQueService.pageMaps(pm, wrapper));
		} else if("1".equals(type)) {
			TBankMultipleChoiceQueQueryCriteria criteria = new TBankMultipleChoiceQueQueryCriteria();
			criteria.setQuestionBankId(questionBankId);
			criteria.setContent(content);
			QueryWrapper wrapper = QueryWrapperUtil.getWrapper(pm, criteria);
			return Result.buildOkData(multipleChoiceQueService.pageMaps(pm, wrapper));
		} else if("2".equals(type)) {
			TBankJudgeQueQueryCriteria criteria = new TBankJudgeQueQueryCriteria();
			criteria.setQuestionBankId(questionBankId);
			criteria.setContent(content);
			QueryWrapper wrapper = QueryWrapperUtil.getWrapper(pm, criteria);
			return Result.buildOkData(judgeQueService.pageMaps(pm, wrapper));
		} else if ("3".equals(type)) {
			TBankFillQueQueryCriteria criteria = new TBankFillQueQueryCriteria();
			criteria.setQuestionBankId(questionBankId);
			criteria.setContent(content);
			QueryWrapper wrapper = QueryWrapperUtil.getWrapper(pm, criteria);
			return Result.buildOkData(fillQueService.pageMaps(pm, wrapper));
		} else if ("4".equals(type)) {
			OjProblemsQueryCriteria criteria = new OjProblemsQueryCriteria();
			criteria.setQuestionBankId(questionBankId);
			criteria.setProblemName(content);
			QueryWrapper wrapper = QueryWrapperUtil.getWrapper(pm, criteria);
			return Result.buildOkData(ojProblemsService.pageMaps(pm, wrapper));
		} else {
			return Result.buildFail("不支持的查询题目类型。");
		}
	}

	@LogOperate(value = "题库题目导出")
	@GetMapping(value = "/download")
	@PreAuthorize("@pms.hasPermission('exam_tQuestionBank_view')")
	public void download(TQuestionBankQueryCriteria tQuestionBankQueryCriteria, HttpServletResponse response) {
//		ExcelUtil<TQuestionBankVo> util = new ExcelUtil(TQuestionBankVo.class);
//		util.exportExcel(service.findPage(tQuestionBankQueryCriteria,
//			SecurityUtil.getDataScope()), "用户数据", response);
	}
}
